package cn.kgc.controller;

import cn.kgc.pojo.Bill;
import cn.kgc.pojo.Provider;
import cn.kgc.pojo.User;
import cn.kgc.service.BillService;
import cn.kgc.service.ProviderService;
import cn.kgc.util.PageBean;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 蒿雨
 * @create 2021-11-01 17:57
 *骑士精神
 */
@Controller
@RequestMapping("/bill")
public class BillController {

    @Resource(name = "bsi")
    BillService billService;
    @Resource(name = "psi")
    ProviderService providerService;


    //修改方法
    @RequestMapping("/modifyBill")
    public String modifyP(Bill bill, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        bill.setModifyBy(user.getId());
        bill.setModifyDate(new Date());
        if (billService.billModify(bill)) {
            System.out.println("修改成功！");
            return "redirect:/bill/billlist";
        }
        return "billmodify";

    }

    //修改赋值
    @RequestMapping("/billmodify/{id}")
    public String modify(@PathVariable int id, HttpSession session) {
        session.setAttribute("bill", billService.getB(id));
        return "billmodify";
    }


    @RequestMapping("/delete1")
    @ResponseBody
    public String userDel(int billid) {
        System.out.println(billid);
        Map<String, Object> map = new HashMap<>();
        if (billService.isFull(billid)) {
            map.put("delResult", "notexist");
            return JSON.toJSONString(map);
        }
        if (billService.deleteB(billid) > 0) {
            map.put("delResult", "true");
            return JSON.toJSONString(map);
        }
        map.put("delResult", "false");
        return JSON.toJSONString(map);
    }

    @RequestMapping("/billview/{id}")
    public String view(@PathVariable int id, HttpSession session) {
        session.setAttribute("bill", billService.getB(id));
        return "billview";
    }


    @RequestMapping("/billlist")
    public String billlist(HttpSession session, HttpServletRequest request, String pageIndex, String queryProviderId, String queryIsPayment, String queryProductName) {
        List<Provider> providers = providerService.PROVIDERS();
        session.setAttribute("providerList", providers);
        System.out.println(queryIsPayment);
        Map<String, Object> map = new HashMap<>();
        int index = 1;
        int pageSize = 5;
        if (pageIndex != null) {
            index = Integer.parseInt(pageIndex);
        }
        int id = 0;
        if (queryProviderId != null) {
            id = Integer.valueOf(queryProviderId);
        }
        int is = 0;
        if (queryIsPayment != null) {
            is = Integer.valueOf(queryIsPayment);
        }
        map.put("queryProviderId", id);
        map.put("queryIsPayment", is);
        map.put("startIndex", index);
        map.put("pageSize", pageSize);
        map.put("queryProductName", queryProductName);
        PageBean<Bill> pageBean = billService.getList(map);
        session.setAttribute("billList", pageBean);
        request.setAttribute("queryProviderId", queryProviderId);
        request.setAttribute("queryIsPayment", queryIsPayment);
        request.setAttribute("queryProductName", queryProductName);
        System.out.println(pageBean.getCurrentPage());
        return "billlist";
    }

    //跳转到添加页面
    @RequestMapping("/billadd")
    public String addgo(HttpSession session) {
//        session.setAttribute("provider", providerService.PROVIDERS());
        return "billadd";
    }

    //获取供应上列表
    @RequestMapping("/billp")
    @ResponseBody
    public String billp() {
        List<Provider> providers = providerService.PROVIDERS();
        return JSON.toJSONString(providers);
    }

    @RequestMapping("/billaddsave")
    public String add(Bill bill, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        bill.setCreatedBy(user.getId());
        bill.setCreationDate(new Date());
        if (billService.addB(bill)) {
            System.out.println("添加成功！");
            return "redirect:/bill/billlist";
        }
        System.out.println("添加失败");
        return "billadd";
    }

}
